package org.malred.hotelsystem.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import org.malred.hotelsystem.model.entity.HotelEntity;
import org.malred.hotelsystem.model.entity.OrderEntity;

import java.util.ArrayList;

public class OrderDao {
    private static final String TAG = "OrderDao";
    private static final String TB_NAME = "tb_order";

    /**
     * 添加
     *
     * @param db
     * @param entity
     * @return
     */
    public static boolean add(SQLiteDatabase db, OrderEntity entity) {
        ContentValues values = new ContentValues();
        values.put("o_id", entity.getId());
        values.put("h_id", entity.getHid());
        values.put("u_id", entity.getUid());
        values.put("o_status", entity.getStatus());
        values.put("o_date", entity.getoDate());
        long i = db.insert(TB_NAME, null, values);
        return i > 0;
    }

    /**
     * 根据id删除
     *
     * @param db
     * @param id
     * @return
     */
    public static boolean del(SQLiteDatabase db, String id) {
        long i = db.delete(TB_NAME, "o_id=?", new String[]{id});
        return i > 0;
    }

    /**
     * 更新
     *
     * @param db
     * @param entity
     * @return
     */
    public static boolean upt(SQLiteDatabase db, OrderEntity entity) {
        ContentValues values = new ContentValues();
        values.put("h_id", entity.getHid());
        values.put("u_id", entity.getUid());
        values.put("o_status", entity.getStatus());
        values.put("o_date", entity.getoDate());
        int i = db.update(TB_NAME, values, "o_id=?", new String[]{entity.getId()});
        return i > 0;
    }

    /**
     * 查询所有
     *
     * @param db
     * @return
     */
    public static ArrayList<OrderEntity> getAll(SQLiteDatabase db) {
        ArrayList<OrderEntity> items = new ArrayList();
        Cursor c = db.query(TB_NAME, null, null,
                null, null, null, null);
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("o_id"));
            @SuppressLint("Range") String hid = c.getString(c.getColumnIndex("h_id"));
            @SuppressLint("Range") String uid = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String status = c.getString(c.getColumnIndex("o_status"));
            @SuppressLint("Range") String date = c.getString(c.getColumnIndex("o_date"));
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setId(id);
            orderEntity.setHid(hid);
            orderEntity.setUid(uid);
            orderEntity.setStatus(status);
            orderEntity.setoDate(date);
            items.add(orderEntity);
        }
        c.close();
        return items;
    }

    /**
     * 查询某用户的所有订单
     *
     * @param db
     * @return
     */
    public static ArrayList<OrderEntity> getAllByUser(SQLiteDatabase db, String uidStr) {
        ArrayList<OrderEntity> items = new ArrayList();
        Cursor c = db.query(TB_NAME, null, "u_id=?",
                new String[]{uidStr}, null, null, null);
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("o_id"));
            @SuppressLint("Range") String hid = c.getString(c.getColumnIndex("h_id"));
            @SuppressLint("Range") String uid = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String status = c.getString(c.getColumnIndex("o_status"));
            @SuppressLint("Range") String date = c.getString(c.getColumnIndex("o_date"));
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setId(id);
            orderEntity.setHid(hid);
            orderEntity.setUid(uid);
            orderEntity.setStatus(status);
            orderEntity.setoDate(date);
            items.add(orderEntity);
        }
        c.close();
        return items;
    }

    /**
     * 根据id查询
     *
     * @param db
     * @param idVal
     * @return
     */
    public static OrderEntity getById(SQLiteDatabase db, String idVal) {
        Cursor c = db.query(
                TB_NAME, null,
                "o_id=?", new String[]{idVal},
                null, null,
                null
        );
        OrderEntity orderEntity = new OrderEntity();
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("o_id"));
            @SuppressLint("Range") String hid = c.getString(c.getColumnIndex("h_id"));
            @SuppressLint("Range") String uid = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String status = c.getString(c.getColumnIndex("o_status"));
            @SuppressLint("Range") String date = c.getString(c.getColumnIndex("o_date"));
            orderEntity.setId(id);
            orderEntity.setHid(hid);
            orderEntity.setUid(uid);
            orderEntity.setStatus(status);
            orderEntity.setoDate(date);
        }
        c.close();
        return orderEntity;
    }

    public static OrderEntity getByHidUid(SQLiteDatabase db, String hotelId, String userId) {
        Cursor c = db.query(
                TB_NAME, null,
                "h_id=? and u_id=?", new String[]{hotelId, userId},
                null, null,
                null
        );
        OrderEntity orderEntity = null;
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("o_id"));
            @SuppressLint("Range") String hid = c.getString(c.getColumnIndex("h_id"));
            @SuppressLint("Range") String uid = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String status = c.getString(c.getColumnIndex("o_status"));
            @SuppressLint("Range") String date = c.getString(c.getColumnIndex("o_date"));
            orderEntity = new OrderEntity();
            orderEntity.setId(id);
            orderEntity.setHid(hid);
            orderEntity.setUid(uid);
            orderEntity.setStatus(status);
            orderEntity.setoDate(date);
        }
        c.close();
        return orderEntity;
    }
}
